package de.unibremen.mum.app.whatscool;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.Toast;
import de.unibremen.mum.app.whatscool.camera.TakePictureActivity;

/**
 * Created with IntelliJ IDEA. User: Marten Date: 11.06.13 Time: 13:51 To change
 * this template use File | Settings | File Templates.
 */
public class HomeFragment extends Fragment implements OnClickListener {
	
	private OnHeadlineSelectedListener mCallback;
	private DatabaseHandler db;
	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View homeView = inflater.inflate(R.layout.activity_start, container, false);
		db = new DatabaseHandler(getActivity());
		ImageButton b = (ImageButton) homeView.findViewById(R.id.button1);
		b.setOnClickListener(this);
        ImageButton b2 = (ImageButton) homeView.findViewById(R.id.button2);
		b2.setOnClickListener(this);
        ImageButton b3 = (ImageButton) homeView.findViewById(R.id.button3);
		b3.setOnClickListener(this);
        ImageButton b4 = (ImageButton) homeView.findViewById(R.id.button4);
		b4.setOnClickListener(this);
		ImageButton b5 = (ImageButton) homeView.findViewById(R.id.whatscool);
		b4.setOnClickListener(this);

		return homeView;
	}
	
	
	// Container Activity must implement this interface
    public interface OnHeadlineSelectedListener {
        public void onPhotoTaken(String url);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        
        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnHeadlineSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.button1:
			Intent takePictureIntent = new Intent(getActivity(), TakePictureActivity.class);
			takePictureIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
            startActivityForResult(takePictureIntent, 0);
			break;
		case R.id.button2:
			long id = -1;
			id = db.getIDlast();
			Intent resultLast = new Intent(getActivity(), ResultActivity.class);
			resultLast.putExtra("id",id);
            startActivity(resultLast);
			break;
		case R.id.button3:
			Toast.makeText(getActivity(), "Aktiv ab Version 2", Toast.LENGTH_SHORT).show();
			break;
		case R.id.button4:
			Toast.makeText(getActivity(), "Aktiv ab Version 2", Toast.LENGTH_SHORT).show();
			break;
		case R.id.whatscool:
			Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.incontextdesign.com/innovationincool/"));
	        startActivity(i);
			break;
		}

	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 0 && resultCode == 42) {
			//Foto URL an MainActivity ��bergeben, damit es von da aus
			//weiter an das RatingFragment ��bergeben werden kann.
			String photoUrl = data.getStringExtra("uri");
			mCallback.onPhotoTaken(photoUrl);
		}
	}	
 
}
